package com.cloudcross.ssp.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.utils.LangUtil;
import com.cloudcross.ssp.common.utils.MapBuilder;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.common.utils.SqlHelper;
import com.cloudcross.ssp.model.OperatorBalance;
import com.cloudcross.ssp.service.IOperatorBalanceService;

@Service
public class OperatorBalanceService implements IOperatorBalanceService {
    @Autowired
    private GenericIBatisDao myBatisDao;

    @Override
    public OperatorBalance findById(Long id) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int countByParamsOperator(Map<String, Object> paramMap) {
        return myBatisDao.get("operatorBalanceSqlMapper.countByParamsOperator", paramMap);
    }

    @Override
    public int countByParams(Map<String, Object> paramMap) {
        // 这里的搜索只是对交易号进行搜索
        String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
        paramMap.put("num", searchValue);
        return myBatisDao.get("operatorBalanceSqlMapper.countByParams", paramMap);
    }

    @Override
    public List<OperatorBalance> findByParamsOperator(Map<String, Object> paramMap, Pager pager) {
        paramMap.put("pager", pager);
        return myBatisDao.getList("operatorBalanceSqlMapper.findByParamsOperator", paramMap);
    }

    @Override
    public List<OperatorBalance> findByParams(Map<String, Object> paramMap, Pager pager) {
        // 这里的搜索只是对交易号进行搜索
        String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
        paramMap.put("num", searchValue);
        paramMap.put("pager", pager);
        return myBatisDao.getList("operatorBalanceSqlMapper.findByParams", paramMap);
    }

    // 导出excel
    @Override
    public List<OperatorBalance> findFinancial(Map<String, Object> paramMap) {

        return myBatisDao.getList("operatorBalanceSqlMapper.findFinancial", paramMap);
    }

    @Override
    public boolean add(OperatorBalance t) {
        myBatisDao.save("operatorBalanceSqlMapper.addOperatorBalance", t);
        return true;
    }

    @Override
    public boolean edit(OperatorBalance t) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean updateStatus(List<Long> idList, int status) {
        myBatisDao.save("operatorBalanceSqlMapper.updateOperatorBalanceStatus",
                MapBuilder.create(HashMap.class).add("state", status).add("idList", idList).map());
        return true;
    }

    @Override
    public Float findAvailableMoney(Long operatorId) {
        return myBatisDao.get("operatorBalanceSqlMapper.findAvailableMoney", operatorId);
    }

    @Override
    public Float findSumAmountByOperatorId(Long operatorId) {
        return myBatisDao.get("operatorBalanceSqlMapper.findSumAmountByOperatorId", operatorId);
    }

    @Override
    public boolean updateStatus(List<Long> idList, String reason, int status) {
        myBatisDao.save("operatorBalanceSqlMapper.updateOperatorBalanceStatus",
                MapBuilder.create(HashMap.class).add("state", status).add("idList", idList).add("reason", reason).map());
        return true;
    }

    @Override
    public boolean moneyBackToOperator(Long id) {
        myBatisDao.save("operatorBalanceSqlMapper.moneyBackToOperator", id);
        return true;
    }

    @Override
    public List<OperatorBalance> findByParams(Map<String, Object> paramMap) {
        return myBatisDao.getList("operatorBalanceSqlMapper.findByParams1", paramMap);
    }

    @Override
    public boolean failureAudit(Map<String, Object> paramMap) {
        String reason = (String) paramMap.get("reason");
        Long[] id = (Long[]) paramMap.get("id");
        // 设置状态为审核不通过并且把原因写入数据库
        updateStatus(LangUtil.array2List(id), reason, 2);

        // 把这些审核不通过的提取金额加回去
        for (int i = 0; i < id.length; i++) {
            moneyBackToOperator(id[i]);
        }

        return true;
    }

}
